package com.jphoebe.plugin.mybatis.form.config;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.jphoebe.plugin.mybatis.form.PluginSettingForm;
import com.jphoebe.plugin.mybatis.service.setting.GlobalSettingService;
import com.jphoebe.plugin.mybatis.utils.PropertiesUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * 插件配置类
 *
 * @author 蒋时华
 * @date 2019/2/18
 */
public class PluginSettingConfig implements SearchableConfigurable {

    private PluginSettingForm pluginSettingForm;
    private Boolean defaultNaviJumpStatus, globalNaviJumpStatus;
    private Integer defaultLanguage, globalLanguage;
    private Integer defaultVaniJumpIcon, globalVaniJumpIcon;
    private boolean isReSet = false;

    public PluginSettingConfig() {
        defaultNaviJumpStatus = PropertiesUtil.getPluginProps().getBool("prop.naviJumpStatus.defaultSelected");
        globalNaviJumpStatus = GlobalSettingService.getInstance().getState().getNaviJumpStatus();

        defaultLanguage = PropertiesUtil.getPluginProps().getInt("prop.language.default");
        globalLanguage = GlobalSettingService.getInstance().getState().getLanguage();

        defaultVaniJumpIcon = PropertiesUtil.getPluginProps().getInt("prop.naviJumpStatus.defaultIcon");
        globalVaniJumpIcon = GlobalSettingService.getInstance().getState().getNaviJumpIcon();

    }

    @NotNull
    @Override
    public String getId() {
        return PropertiesUtil.getPluginContextProps().getStr("plugin.name");
    }

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return this.getId();
    }

    @Nullable
    @Override
    public Runnable enableSearch(String option) {
        return null;
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return this.getId();
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        // 初始化 插件配置
        if (null == pluginSettingForm){
            this.pluginSettingForm = new PluginSettingForm();
        }
        // 跳转导航栏是否开启
        if(globalNaviJumpStatus){
            this.pluginSettingForm.getCloseNaviJumpButton().setSelected(false);
            this.pluginSettingForm.getOpenNaviJumpButton().setSelected(true);
            this.pluginSettingForm.getNaviIcon().setEnabled(true);
        }else{
            this.pluginSettingForm.getCloseNaviJumpButton().setSelected(true);
            this.pluginSettingForm.getOpenNaviJumpButton().setSelected(false);
            this.pluginSettingForm.getNaviIcon().setEnabled(false);
        }
        // 跳转导航栏图标
        this.pluginSettingForm.getNaviIcon().setSelectedIndex(globalVaniJumpIcon);
        // 语言
        this.pluginSettingForm.getLanguage().setSelectedIndex(globalLanguage);

        return this.pluginSettingForm.getMainPanel();
    }

    @Override
    public boolean isModified() {
        Boolean currentNaviJumpOpen = this.pluginSettingForm.getOpenNaviJumpButton().isSelected();
        Integer currentVaniJumpIcon = this.pluginSettingForm.getNaviIcon().getSelectedIndex();
        Integer currentlanguage = this.pluginSettingForm.getLanguage().getSelectedIndex();
        isReSet = true;
        if (!globalNaviJumpStatus.equals(currentNaviJumpOpen)){
            return true;
        }
        if (!globalVaniJumpIcon.equals(currentVaniJumpIcon)){
            return true;
        }
        if (!globalLanguage.equals(currentlanguage)){
            return true;
        }
        isReSet = false;
        return false;
    }

    @Override
    public void apply() throws ConfigurationException {

        boolean isNaviJumpOpen = this.pluginSettingForm.getOpenNaviJumpButton().isSelected();
        Integer currentVaniJumpIcon = this.pluginSettingForm.getNaviIcon().getSelectedIndex();
        Integer currentlanguage = this.pluginSettingForm.getLanguage().getSelectedIndex();

        // 保存全局设置信息
        GlobalSettingService.getInstance().setNaviJumpStatus(isNaviJumpOpen);
        GlobalSettingService.getInstance().setNaviJumpIcon(currentVaniJumpIcon);
        GlobalSettingService.getInstance().setLanguage(currentlanguage);
        globalNaviJumpStatus = GlobalSettingService.getInstance().getState().getNaviJumpStatus();
        globalLanguage = GlobalSettingService.getInstance().getState().getLanguage();
        globalVaniJumpIcon = GlobalSettingService.getInstance().getState().getNaviJumpIcon();

        this.pluginSettingForm.setIl8nText(GlobalSettingService.getInstance().getState());
        this.pluginSettingForm.getNaviIcon().setSelectedIndex(currentVaniJumpIcon);
        this.pluginSettingForm.getLanguage().setSelectedIndex(currentlanguage);

    }

    @Override
    public void reset() {
        if(!isReSet){
            return ;
        }
        // 重置设置
        GlobalSettingService.getInstance().setNaviJumpStatus(defaultNaviJumpStatus);
        GlobalSettingService.getInstance().setNaviJumpIcon(defaultVaniJumpIcon);
        GlobalSettingService.getInstance().setLanguage(defaultLanguage);

        this.pluginSettingForm.setIl8nText(GlobalSettingService.getInstance().getState());

        if(defaultNaviJumpStatus){
            this.pluginSettingForm.getCloseNaviJumpButton().setSelected(false);
            this.pluginSettingForm.getOpenNaviJumpButton().setSelected(true);
            this.pluginSettingForm.getNaviIcon().setEnabled(true);
        }else{
            this.pluginSettingForm.getCloseNaviJumpButton().setSelected(true);
            this.pluginSettingForm.getOpenNaviJumpButton().setSelected(false);
            this.pluginSettingForm.getNaviIcon().setEnabled(false);
        }
        this.pluginSettingForm.getNaviIcon().setSelectedIndex(defaultVaniJumpIcon);
        this.pluginSettingForm.getLanguage().setSelectedIndex(defaultLanguage);

        isReSet = false;

    }

}
